package org.xlx.xlx_oa.attendance.controller;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.xlx.xlx_oa.attendance.api.AttendanceApplyApi;
import org.xlx.xlx_oa.attendance.domain.dto.AttendanceApplyDto;
import org.xlx.xlx_oa.attendance.domain.vo.AttendanceApplyVo;
import org.xlx.xlx_oa.attendance.service.AttendanceApplyService;
import org.xlx.xlx_oa.common.BusinessException;
import org.xlx.xlx_oa.common.ResponseBean;
import org.xlx.xlx_oa.role_user.domain.dto.AdmProcessDto;
import org.xlx.xlx_oa.role_user.domain.vo.AuditVo;

import java.util.List;

@RestController
@RequestMapping("/v1/attendance-apply")
public class AttendanceApplyController implements AttendanceApplyApi {
    @Autowired
    private AttendanceApplyService attendanceApplyService;

    @PostMapping("/saveApply")
    @Override
    public ResponseBean saveAttendanceApply(@RequestBody AttendanceApplyVo attendanceApplyVo) {
        try {
            return ResponseBean.successFactory(attendanceApplyService.saveAttendanceApply(attendanceApplyVo));
        } catch (BusinessException e) {
            return ResponseBean.failureFactory(e.getCode(), e.getMessage());
        } catch (Exception e) {
            return ResponseBean.failureFactory(e.getMessage());
        }
    }

    @PostMapping("/audit")
    @Override
    public ResponseBean<Boolean> auditApply(@RequestBody AuditVo auditVo) {
        return ResponseBean.successFactory(attendanceApplyService.auditAttendanceApply(auditVo));
    }

    @PostMapping("/beAuditList")
    @Override
    public ResponseBean<List<AttendanceApplyDto>> getBeAuditPage(@RequestBody AttendanceApplyVo applyVo) {
        List<AttendanceApplyDto> beAuditList = attendanceApplyService.getBeAuditList(applyVo);
        return ResponseBean.successFactory(beAuditList);
    }

    @PostMapping("/beAuditedCount")
    @Override
    public ResponseBean<Integer> getBeAuditedCount(@RequestBody AttendanceApplyVo applyVo) {
        Integer beAuditedCount = attendanceApplyService.getBeAuditedCount(applyVo);
        return ResponseBean.successFactory(beAuditedCount);
    }

    @PostMapping("/appliedList")
    @Override
    public ResponseBean<List<AttendanceApplyDto>> getAppliedPage(@RequestBody AttendanceApplyVo applyVo) {
        List<AttendanceApplyDto> applyList = attendanceApplyService.getApplyList(applyVo);
        return ResponseBean.successFactory(applyList);
    }

    @PostMapping("/appliedCount")
    @Override
    public ResponseBean<Integer> getAppliedCount(@RequestBody AttendanceApplyVo applyVo) {
        Integer applyCount = attendanceApplyService.getApplyCount(applyVo);
        return ResponseBean.successFactory(applyCount);
    }

    @GetMapping("/detail/{applyId}")
    @Override
    public ResponseBean<List<AdmProcessDto>> getApplyDetail(@PathVariable(value = "applyId") Long applyId) {
        List<AdmProcessDto> applyDetail = attendanceApplyService.getApplyDetail(applyId);
        return ResponseBean.successFactory(applyDetail);
    }
}

